package tools;

import entity.DefinedRuntimeException;

/**
 * 用于关闭AutoCloseable资源的工具类.
 * @author Rex
 *
 */
public final class CloseSource {

	/**
	 * 用于接收lambda表达式的接口.
	 * @author Rex
	 *
	 */
	public interface Close {
		
		/**
		 * 接收CloseSource的close(AutoCloseable...)静态方法.
		 * @see CloseSource#close(AutoCloseable...)
		 * @param acs
		 * @return CloseSource::close
		 */
		Close close(AutoCloseable... acs);
		
	}

	/**
	 * 关闭AutoCloseable资源.
	 * @param acs
	 * @return 以lambad表达式形式返回方法自身
	 */
	public static Close close(AutoCloseable... acs) {
		if (acs != null)
			for (AutoCloseable ac : acs)
				if (ac != null)
					try {
						ac.close();
					} catch (Exception e) {
						// 异常不抛出，使用DefinedRuntimeException的构造器写出日志到磁盘
						new DefinedRuntimeException("关闭资源失败", e);
					}
		
		return CloseSource::close;
	}

}